package com.neusoft.movie.v_movies.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.CursorJoiner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.ArticlesAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.ArticlesBean;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.javabean.UsersBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.CollectStateModel;
import com.neusoft.movie.v_movies.model.UsersModel;

import java.util.List;

public class AuthorAllArtActivity extends BaseActivity implements RetrofitListener {

    RecyclerView recyclerView;
    private ImageView left,button_col_author;
    private int u_id,collect_id,collect_flag,author_id,userID;
    private Context mContext;
    private TextView u_name,u_personal;
    private String result;


    @Override
    void initEvents() {

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        button_col_author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addcollect();
            }
        });

    }

    @Override
    void initViews() {
        setLayout(R.layout.activity_author_all_art);

        recyclerView = (RecyclerView) findViewById(R.id.article_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        left=(ImageView) findViewById(R.id.left_button);
        button_col_author=(ImageView) findViewById(R.id.button_col_author);

        u_name=(TextView) findViewById(R.id.u_name);
        u_personal=(TextView) findViewById(R.id.u_personal);
    }

    @Override
    void initData() {

        Bundle bundle = this.getIntent().getExtras();
        author_id = bundle.getInt("u_id");

        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userID = sp.getInt("u_id",0);

        collectstate();
        getUserInfo();
        getMyArticleslist();
    }

    void getUserInfo()
    {

        UsersModel usersModel = new UsersModel();
        usersModel.getUserInfo(author_id,this);
    }

    void getMyArticleslist()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.getMyArticleslist(author_id,this);
    }

    private void collectstate()
    {
        CollectStateModel collectStateModel=new CollectStateModel();
        collectStateModel.collectstate(userID,author_id,2,this);
    }

    private void addcollect()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcollect(userID,author_id,2,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag) {
            case Constants.GETUSERINFO:
                UsersBean usersBean=(UsersBean) o;
                u_name.setText(usersBean.getU_name());
                u_personal.setText(usersBean.getU_personal());
                break;

            case Constants.GETMYARTICLESLIST:
                List<ArticlesBean> list = (List<ArticlesBean>) o;
                ArticlesAdapter adapter = new ArticlesAdapter(this, list);
                recyclerView.setAdapter(adapter);
                break;

            case Constants.ISCOLLECT:
                //获取收藏状态
                ResultBean status=(ResultBean)o;
                result=status.getSuccess();
                if(status.getSuccess().equals("0")){
                    button_col_author.setImageResource(R.drawable.ic_collect);  //没收藏
                }
                else
                {
                    button_col_author.setImageResource(R.drawable.ic_collect_1);  //收藏
                }

                break;


            case Constants.ADDCOLLECT:
                if (((ResultBean)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        button_col_author.setImageResource(R.drawable.ic_collect_1);
                        Toast.makeText(AuthorAllArtActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        button_col_author.setImageResource(R.drawable.ic_collect);
                        Toast.makeText(AuthorAllArtActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(AuthorAllArtActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    @Override
    public void onFailed() {

    }

}